package cn.xyfmzmy.spring.web;

import java.lang.reflect.Method;

/**
 * 请求路径对应的信息的封装
 */
public class WebHandler {

    // Controller对象
    private final Object controllerBean;

    // 具体对应的方法
    private final Method method;

    // 返回值类型
    private final ResultType resultType;

    public WebHandler(Object controllerBean, Method method) {
        this.controllerBean = controllerBean;
        this.method = method;
        this.resultType = resovleResultType(controllerBean,method);
    }

    // 根据controller和method确定返回值类型
    private ResultType resovleResultType(Object controllerBean, Method method) {
        if(method.isAnnotationPresent(ResponseBody.class)){
            return ResultType.JSON;
        }
        if(method.getReturnType() == ModelAndView.class){
            return ResultType.LOCAL;
        }
        return ResultType.HTML;
    }

    public Object getControllerBean() {
        return controllerBean;
    }

    public Method getMethod() {
        return method;
    }

    public ResultType getResultType() {
        return resultType;
    }

    // 返回类型，json/html/本地静态资源转html
    enum ResultType {
        JSON,HTML,LOCAL
    }
}
